package com.sundi.ecmarket.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.dto.BrandDto;
import com.sundi.ecmarket.dto.IssueDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcBrand;
import com.sundi.ecmarket.service.IEcBrandService;
import com.sundi.ecmarket.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.ResultMapResolver;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 品牌商表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@Slf4j
@RestController
@RequestMapping("/ecBrand")
public class EcBrandController {


    @Autowired
    IEcBrandService iEcBrandService;

    @Autowired
    UploadUtils uploadUtils;

    /**
     * @deprecated ec商城中商场管理里的品牌制造商列表模块
     * @author 许家宝
     * @param brandDto
     * @return
     */
    @RequestMapping("/xujiabao/brandlist")
    public ResultEntity list(BrandDto brandDto){

        PageHelper.startPage(brandDto.getPageNo(),brandDto.getPageSize());
        List<EcBrand> list = iEcBrandService.selectBrandList(brandDto);
        PageInfo<EcBrand> pageInfo = new PageInfo(list);
        return ResultEntity.ok(pageInfo);
    }


    /**
     * @deprecated  ec商城中商场管理里的品牌制造商添加/修改模块
     * @author 许家宝
     * @param brandDto
     * @return
     */
    @RequestMapping("/xujiabao/brandAddOrUpd")
    public ResultEntity add(BrandDto brandDto) throws IOException {

        System.err.println(brandDto);
        if(brandDto.getFiles()!=null){
            List<String> upload = uploadUtils.upload(brandDto.getFiles());
            String s = upload.get(0);
            brandDto.setPicUrl(s);
            brandDto.setFiles(null);
        }
        EcBrand ecBrand = new EcBrand();
        BeanUtils.copyProperties(brandDto,ecBrand);
        if(ecBrand.getId()!=null){
            ecBrand.setUpdateTime(new Date());
        }else{
            ecBrand.setAddTime(new Date());
        }
        boolean b = iEcBrandService.insertOrUpdate(ecBrand);
        log.info("----------------添加/修改{}", JSON.toJSONString(b));
        return ResultEntity.ok(b);
    }

    /**
     * @deprecated ec商城中商场管理里的品牌制造商查询具体每条数据模块
     * @author 许家宝
     * @param id
     * @return
     */
    @RequestMapping("/xujiabao/getBrandById")
    public ResultEntity upd(Integer id){

        EcBrand ecBrand = iEcBrandService.selectById(id);
        return ResultEntity.ok(ecBrand);
    }


    /**
     * @deprecated ec商城中商场管理里的品牌制造商逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
    @RequestMapping("/xujiabao/logicdel")
    public ResultEntity logindel(Integer id){

        System.err.println(id);
        iEcBrandService.updateLogicDel(id);
        return ResultEntity.ok();
    }

    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(BrandDto brandDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = iEcBrandService.downLoadGoodsExcel(brandDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("品牌制造商表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }
}
